
package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultCaret;

import Satellites.InvalidTleException;
import Satellites.Orbit;
import Satellites.SatNotFoundException;
import Satellites.Satellite;
import coreEngine.ArduinoConnector;
import coreEngine.Chronometer;
import coreEngine.Clock;
import coreEngine.GlobalVariables;
import coreEngine.LogInfo;
import coreEngine.MovingAntenna;

public class SatelliteTracker extends JFrame{

	// Declaracao de variaveis                     
	private JButton trackingButton;
	private JButton stopButton;
	private JButton connectArduinoButton;
	private JButton availableButton;
	private JButton refreshButton;
	private JComboBox<Satellite> satelliteComboBox;
	private JPanel informationPanel, jPanel4, jPanel5, jPanel6;
	private JScrollPane jScrollPane1;
	private JTextArea jTextArea1;
	private JTextField jTextField2,jTextField3,satelliteNumber,jTextField5,jTextField6,jTextField7,jTextField8
	,jTextField9,jTextField10,jTextField11,jTextField12,jTextChronometerField,jTextClockField;

	private ArduinoConnector ac;

	private JPanel panelWithTextArea = new JPanel();
	private JTextArea textAreaInfo = new JTextArea();
	private JScrollPane scrollWithTextArea;
	private DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
	private Date date;
	private Clock clock;
	private Chronometer timer;
	private MovingAntenna ma;
	private static final long serialVersionUID = 1L;

	private Image icon = new ImageIcon("others/Logo.png").getImage();
	ImageIcon planisferio = new ImageIcon("others/planisferio.jpeg");

	private MainGUI mainGUI;


	public SatelliteTracker(String title, MainGUI mainGUI) {
		this.mainGUI = mainGUI;
		setTitle(title + " Tracker");
		setIconImage(icon);
		clock = new Clock(SatelliteTracker.this);



		ac = new ArduinoConnector(SatelliteTracker.this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBackground(new java.awt.Color(0, 0, 0));

		informationPanel = new JPanel();
		jTextField2 = new JTextField();
		satelliteNumber = new JTextField();
		jTextField5 = new JTextField();
		jTextField6 = new JTextField();
		jTextField7 = new JTextField();
		jTextField8 = new JTextField();
		jTextField9 = new JTextField();
		jTextField10 = new JTextField();
		jTextField11 = new JTextField();
		jTextField12 = new JTextField();
		jTextChronometerField = new JTextField();
		jTextClockField = new JTextField();
		jScrollPane1 = new JScrollPane();
		jTextArea1 = new JTextArea();
		jPanel4 = new JPanel();
		satelliteComboBox = new JComboBox<Satellite>();
		jPanel6 = new JPanel();
		jTextField3 = new JTextField();
		jPanel5 = new JPanel();
		trackingButton = new JButton();
		stopButton = new JButton();
		connectArduinoButton = new JButton();
		availableButton = new JButton();
		refreshButton = new JButton();

		trackingButton.setEnabled(false); //Go
		stopButton.setEnabled(false); //STOP
		connectArduinoButton.setEnabled(true); // Info
		availableButton.setEnabled(true); //Available
		satelliteComboBox.setEnabled(true);


		if (!GlobalVariables.internetConnectionAvailable){
			refreshButton.setEnabled(false); //Refresh
		}
		else {
			refreshButton.setEnabled(true); //Refresh
		}

		initComponents(mainGUI);

		setSize(850, 605);
		//		pack();
		setResizable(false);
		setLocationRelativeTo(null);
	}


	/**
	 * Construcao do GUI
	 * @param mainGUI
	 */
	@SuppressWarnings("unchecked")
	private void initComponents(MainGUI mainGUI) {
		textAreaInfo.setColumns(20);
		textAreaInfo.setRows(5);
		textAreaInfo.setEditable(false);


		DefaultCaret caret = (DefaultCaret)textAreaInfo.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		textAreaInfo.setBackground(new Color(200,200,200));

		scrollWithTextArea = new JScrollPane(textAreaInfo);
		scrollWithTextArea.setViewportView(textAreaInfo);

		panelWithTextArea.setBackground(Color.BLACK);



		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		GroupLayout textAreaInfoLayout = new GroupLayout(panelWithTextArea);
		panelWithTextArea.setLayout(textAreaInfoLayout);

		textAreaInfoLayout.setHorizontalGroup(
				textAreaInfoLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(textAreaInfoLayout.createSequentialGroup().addComponent(scrollWithTextArea)));


		textAreaInfoLayout.setVerticalGroup(
				textAreaInfoLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(textAreaInfoLayout.createSequentialGroup().addComponent(scrollWithTextArea)));


		informationPanel.setBackground(new java.awt.Color(0, 0, 0));
		//TextFiel Lateral Esquerda
		//Onde se vai inserir informa��o sobre...

		jTextField2.setEditable(false);
		jTextField2.setBackground(new java.awt.Color(0, 0, 0));
		jTextField2.setFont(new java.awt.Font("Tahoma", 1, 11)); 
		jTextField2.setForeground(new java.awt.Color(0, 204, 0));
		jTextField2.setHorizontalAlignment(JTextField.CENTER);
		jTextField2.setText("Satellite Information");
		jTextField2.setBorder(null);



		//Informacao dos Satelites
		satelliteNumber.setEditable(false);
		satelliteNumber.setBackground(new java.awt.Color(0, 0, 0));
		satelliteNumber.setForeground(new java.awt.Color(0, 204, 0));
		satelliteNumber.setHorizontalAlignment(JTextField.LEFT);
		satelliteNumber.setText("Satellite Number:");
		satelliteNumber.setBorder(null);

		jTextField5.setEditable(false);
		jTextField5.setBackground(new java.awt.Color(0, 0, 0));
		jTextField5.setForeground(new java.awt.Color(0, 204, 0));
		jTextField5.setHorizontalAlignment(JTextField.LEFT);
		jTextField5.setText("Classification:");
		jTextField5.setBorder(null);

		jTextField6.setEditable(false);
		jTextField6.setBackground(new java.awt.Color(0, 0, 0));
		jTextField6.setForeground(new java.awt.Color(0, 204, 0));
		jTextField6.setHorizontalAlignment(JTextField.LEFT);
		jTextField6.setText("Above Horizon? ");
		jTextField6.setBorder(null);

		jTextField7.setEditable(false);
		jTextField7.setBackground(new java.awt.Color(0, 0, 0));
		jTextField7.setForeground(new java.awt.Color(0, 204, 0));
		jTextField7.setHorizontalAlignment(JTextField.LEFT);
		jTextField7.setText("i [Degrees]:");
		jTextField7.setBorder(null);

		jTextField8.setEditable(false);
		jTextField8.setBackground(new java.awt.Color(0, 0, 0));
		jTextField8.setForeground(new java.awt.Color(0, 204, 0));
		jTextField8.setHorizontalAlignment(JTextField.LEFT);
		jTextField8.setText("Eccentricity:");
		jTextField8.setBorder(null);

		jTextField9.setEditable(false);
		jTextField9.setBackground(new java.awt.Color(0, 0, 0));
		jTextField9.setForeground(new java.awt.Color(0, 204, 0));
		jTextField9.setHorizontalAlignment(JTextField.LEFT);
		jTextField9.setText("omega [Degrees]:");
		jTextField9.setBorder(null);

		jTextField10.setEditable(false);
		jTextField10.setBackground(new java.awt.Color(0, 0, 0));
		jTextField10.setForeground(new java.awt.Color(0, 204, 0));
		jTextField10.setHorizontalAlignment(JTextField.LEFT);
		jTextField10.setText("W [Degrees]:");
		jTextField10.setBorder(null);

		jTextField11.setEditable(false);
		jTextField11.setBackground(new java.awt.Color(0, 0, 0));
		jTextField11.setForeground(new java.awt.Color(0, 204, 0));
		jTextField11.setHorizontalAlignment(JTextField.LEFT);
		jTextField11.setText("M  [Degrees]:");
		jTextField11.setBorder(null);

		jTextField12.setEditable(false);
		jTextField12.setBackground(new java.awt.Color(0, 0, 0));
		jTextField12.setForeground(new java.awt.Color(0, 204, 0));
		jTextField12.setHorizontalAlignment(JTextField.LEFT);
		jTextField12.setText("N [Revs per day]:");
		jTextField12.setBorder(null);

		jTextChronometerField.setEditable(false);
		jTextChronometerField.setBackground(new java.awt.Color(0, 0, 0));
		jTextChronometerField.setForeground(new java.awt.Color(0, 204, 0));
		jTextChronometerField.setHorizontalAlignment(JTextField.LEFT);
		jTextChronometerField.setText("SAT VISIBLE IN: 00:00:00");
		jTextChronometerField.setBorder(null);

		jTextClockField.setEditable(false);
		jTextClockField.setBackground(new java.awt.Color(0, 0, 0));
		jTextClockField.setForeground(new java.awt.Color(0, 204, 0));
		jTextClockField.setHorizontalAlignment(JTextField.LEFT);
		jTextClockField.setText("TIME: " + clock.getTime());
		jTextClockField.setBorder(null);

		jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
		jTextArea1.setColumns(20);
		jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
		jTextArea1.setForeground(new java.awt.Color(0, 204, 0));
		jTextArea1.setRows(5);
		jTextArea1.setText("i - Inclination\nomega - Right Ascension of \nthe Ascending Node\nw - Argument of Perigee\nM - Mean Anomaly\nN - Mean Motion [Revs per day]\nr - Revolution number at epoch");
		jScrollPane1.setViewportView(jTextArea1);



		//Painel 3 - Para legendar as informa��es dos Satelites
		GroupLayout informationPanelLayout = new GroupLayout(informationPanel);
		informationPanel.setLayout(informationPanelLayout);
		informationPanelLayout.setHorizontalGroup(
				informationPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(informationPanelLayout.createSequentialGroup()
						.addGroup(informationPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(informationPanelLayout.createSequentialGroup()
										.addGroup(informationPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
												.addGroup(informationPanelLayout.createSequentialGroup()
														.addGap(47, 47, 47)
														.addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
														.addGroup(informationPanelLayout.createSequentialGroup()
																.addContainerGap()
																.addComponent(satelliteNumber, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																.addGroup(informationPanelLayout.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(jTextField5, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																		.addGroup(informationPanelLayout.createSequentialGroup()
																				.addContainerGap()
																				.addComponent(jTextField6, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																				.addGroup(informationPanelLayout.createSequentialGroup()
																						.addContainerGap()
																						.addComponent(jTextField7, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																						.addGroup(informationPanelLayout.createSequentialGroup()
																								.addContainerGap()
																								.addComponent(jTextField8, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE))
																								.addGroup(informationPanelLayout.createSequentialGroup()
																										.addContainerGap()
																										.addComponent(jTextField9, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																										.addGroup(informationPanelLayout.createSequentialGroup()
																												.addContainerGap()
																												.addComponent(jTextField10, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																												.addGroup(informationPanelLayout.createSequentialGroup()
																														.addContainerGap()
																														.addComponent(jTextField11, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																														.addGroup(informationPanelLayout.createSequentialGroup()
																																.addContainerGap()
																																.addComponent(jTextField12, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																																.addGroup(informationPanelLayout.createSequentialGroup()
																																		.addContainerGap()
																																		.addComponent(jTextChronometerField, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
																																		.addGroup(informationPanelLayout.createSequentialGroup()
																																				.addContainerGap()
																																				.addComponent(jTextClockField, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)))
																																				.addGap(0, 58, Short.MAX_VALUE))
																																				.addGroup(informationPanelLayout.createSequentialGroup()
																																						.addContainerGap()
																																						.addComponent(jScrollPane1)))
																																						.addContainerGap())
				);

		informationPanelLayout.setVerticalGroup(
				informationPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(informationPanelLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(26, 26, 26)
						.addComponent(satelliteNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(13, 13, 13)
						.addComponent(jTextField11, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextField12, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(11, 11, 11)
						.addComponent(jTextChronometerField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jTextClockField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
						.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
				);



		//Painel 4 - Referente a Combo Box
		jPanel4.setBackground(new java.awt.Color(0, 0, 0));
		satelliteComboBox.setBackground(new java.awt.Color(0, 0, 0));
		satelliteComboBox.setForeground(new java.awt.Color(0, 204, 0));
		satelliteComboBox.setPreferredSize(new Dimension(satelliteComboBox.getPreferredSize().height, 140));
		satelliteComboBox.addItem(null);
		for (int i =0; i != mainGUI.getComm().getSatelliteList().size(); i++){
			satelliteComboBox.addItem(mainGUI.getComm().getSatelliteList().get(i));
		}

		satelliteComboBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jComboBox2ActionPerformed(evt);
			}
		});

		jPanel6.setBackground(new java.awt.Color(0, 0, 0));

		//TextField no Painel Superior Esquerdo
		jTextField3.setEditable(false);
		jTextField3.setBackground(new java.awt.Color(0, 0, 0));
		jTextField3.setForeground(new java.awt.Color(0, 204, 0));
		jTextField3.setHorizontalAlignment(JTextField.CENTER);
		jTextField3.setText("Choose Satellite");
		jTextField3.setBorder(null);

		GroupLayout jPanel6Layout = new GroupLayout(jPanel6);

		//Painel 6 - Onde est� a ComboBox e a TextField3
		jPanel6.setLayout(jPanel6Layout);
		jPanel6Layout.setHorizontalGroup(
				jPanel6Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel6Layout.createSequentialGroup()
						.addGap(61, 61, 61)
						.addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(85, Short.MAX_VALUE))
				);
		jPanel6Layout.setVerticalGroup(
				jPanel6Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel6Layout.createSequentialGroup()
						.addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(0, 45, Short.MAX_VALUE))
				);

		//Para alinhar tudo no painel Superior Esquerdo
		//Label por cima e por baixo a ComboBox
		GroupLayout jPanel4Layout = new GroupLayout(jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(
				jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel4Layout.createSequentialGroup()
						.addGap(19, 19, 19)
						.addComponent(satelliteComboBox, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(jPanel4Layout.createSequentialGroup()
										.addContainerGap()
										.addComponent(jPanel6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
				);
		jPanel4Layout.setVerticalGroup(
				jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(satelliteComboBox, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
						.addGap(19, 19, 19))
						.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(jPanel4Layout.createSequentialGroup()
										.addContainerGap()
										.addComponent(jPanel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addContainerGap()))
				);


		//Painel 5 - Painel topo Direito
		// Onde est�o os Butons

		jPanel5.setBackground(new java.awt.Color(0, 0, 0));

		trackingButton.setBackground(new java.awt.Color(0, 0, 0));
		trackingButton.setForeground(new java.awt.Color(0, 204, 0));
		trackingButton.setText("Track");

		stopButton.setBackground(new java.awt.Color(0, 0, 0));
		stopButton.setForeground(new java.awt.Color(0, 204, 0));
		stopButton.setText("Stop");
		stopButton.setMaximumSize(new java.awt.Dimension(45, 23));
		stopButton.setMinimumSize(new java.awt.Dimension(45, 23));


		connectArduinoButton.setBackground(new java.awt.Color(0, 0, 0));
		connectArduinoButton.setForeground(new java.awt.Color(0, 204, 0));
		connectArduinoButton.setText("Arduino Connector");

		availableButton.setBackground(new java.awt.Color(0, 0, 0));
		availableButton.setForeground(new java.awt.Color(0, 204, 0));
		availableButton.setText("Available");

		refreshButton.setBackground(new java.awt.Color(0, 0, 0));
		refreshButton.setForeground(new java.awt.Color(0, 204, 0));
		refreshButton.setText("Refresh Data");

		GroupLayout jPanel5Layout = new GroupLayout(jPanel5);
		jPanel5.setLayout(jPanel5Layout);
		jPanel5Layout.setHorizontalGroup(
				jPanel5Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel5Layout.createSequentialGroup()
						.addGap(23, 23, 23)
						.addComponent(trackingButton, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(stopButton, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
						.addGap(10, 10, 10)
						.addComponent(availableButton)
						.addGap(10, 10, 10)
						.addComponent(refreshButton)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(connectArduinoButton)
						.addContainerGap())
				);
		jPanel5Layout.setVerticalGroup(
				jPanel5Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel5Layout.createSequentialGroup()
						.addGap(19, 19, 19)
						.addGroup(jPanel5Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(trackingButton, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
								.addComponent(stopButton, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
								.addComponent(availableButton)
								.addComponent(connectArduinoButton)
								.addComponent(refreshButton))
								.addContainerGap(28, Short.MAX_VALUE))
				);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
								.addComponent(jPanel4, GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
								.addComponent(informationPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGap(1, 1, 1)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(panelWithTextArea, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(jPanel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
								.addComponent(jPanel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jPanel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGap(1, 1, 1)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
										.addComponent(informationPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(panelWithTextArea, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
				);




		//***********************************************************************************************************************************************************************************************
		//************ Eventos **********************************************************************************************************************************************************************

		//Evento Track
		trackingButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if(ac.isArduinoConnected()){
					trackingButton.setEnabled(false); //Go
					satelliteComboBox.setEnabled(false);
					refreshButton.setEnabled(false);
					stopButton.setEnabled(true); //STOP
					connectArduinoButton.setEnabled(false); // Info
					availableButton.setEnabled(false); //Available

					refreshButton.setEnabled(false); //Refresh
					appendText("Arduino Connection.....  OK");
					appendText("Tracking satellite "+satelliteComboBox.getSelectedItem());
					try {
						timer = new Chronometer(SatelliteTracker.this, satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().timeUntilPassing());
						appendText("Countdown Started... ETA is: " + Chronometer.getTimeLeft());
						ac.sendData(satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getFirstAzimuth() + "_0.0");
						appendText("Preparing Antenna For Tracking");
						appendText("Moving Antenna to: Elevation = 0.0 Azimuth = " + satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getFirstAzimuth());
						ma = new MovingAntenna(SatelliteTracker.this);
						//enviar para o arduino connector as coordenadas iniciais
						ac.sendData(satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getFirstAzimuth() + "_0.0");
						ma.run();
						//TODO enviar elevacao e azimuth para o arduino

					} catch (InvalidTleException | SatNotFoundException e1) {
						e1.printStackTrace();
					}
				}else{

					appendText("[ERROR] Check Arduino Connection!");
				}
			}
		});

		//Evento Button Stop
		stopButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				trackingButton.setEnabled(true); //Go
				stopButton.setEnabled(false); //STOP
				connectArduinoButton.setEnabled(true); // Info
				availableButton.setEnabled(true); //Available
				refreshButton.setEnabled(true); //Refresh
				satelliteComboBox.setEnabled(true);
				//Cronometro colocado a zero
				timer = new Chronometer(SatelliteTracker.this, 0);
				ac.closeConnection();
			}
		});


		/**
		 * Evento para conectar com o Arduino
		 */
		connectArduinoButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ac.startConnection();
			}
		});

		/**
		 * Evento para saber se o satelite esta disponivel
		 */
		availableButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (satelliteComboBox.getSelectedIndex()==-1){
					appendText("[ERROR] Please select a satellite first!!!");
				}
				else{
					if(ac.getConnectionIsOpen()){
						trackingButton.setEnabled(true);
					}
					appendText(satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex())+" is now available for tracking.");
					satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().CalculateSatelliteParameters();
					try {
						appendText("[INFO]\n"+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().positionPrediction());
					} catch (InvalidTleException | SatNotFoundException e1) {
						e1.printStackTrace();
					}
				}

			}
		});

		/**
		 * Evento para fazer novo ir buscar os novos dados a Net
		 */
		refreshButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(getMainGUI().checkConnection()){
					getMainGUI().getComm().retrieveDataFromURL(GlobalVariables.constellationName, GlobalVariables.link);
					satelliteComboBox.removeAllItems();
					satelliteComboBox.addItem(null);
					for (int i = 0; i != getMainGUI().getComm().getSatelliteList().size();i++){
						satelliteComboBox.addItem(getMainGUI().getComm().getSatelliteList().get(i));
					}
					resetInformationFields();
					appendText("[INFO] The Satellite's list was updated");


				}
				else{
					appendText("[ERROR] No Internet connection available");
				}

			}


		});
		satelliteComboBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED && satelliteComboBox.getSelectedIndex()!=0){
					updateSatelliteNumber(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getSatNumber()); 
					updateClassification(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getClassification());
					updateIsAboveHorizon(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().isSatelliteVisible());
					updateEccentricity(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getEccentricity());
					updateI(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getInclination());
					updateW(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getArgPerigee());
					updateM(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getMeanAnomaly());
					updateN(""+satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getRevs());
					double auxOmega = satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getOmega();
					double auxOmega2decimal = (double) Math.round((auxOmega) * 100)/100;
					updateOmega(""+auxOmega2decimal);

					//TODO falta preencher mais uns campos, mas tu deves saber melhor do que eu quais sao....

				}
				else{
					resetInformationFields();
				}


			}
		});



		addWindowListener(new WindowListener() {



			@Override
			public void windowClosing(WindowEvent e) {
				LogInfo li = new LogInfo(textAreaInfo);
				li.writeLogsToFile();				
				e.getWindow().dispose();

			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}


		});
	}

	public void resetInformationFields(){
		updateSatelliteNumber(""); 
		updateClassification("");
		updateIsAboveHorizon("");
		updateEccentricity("");
		updateI("");
		updateW("");
		updateM("");
		updateN("");
		updateOmega("");
	}


	//**********************************************************
	//**************** Update Info Satelites*******************

	public void updateSatelliteNumber(String update){

		satelliteNumber.setText("Satellite Number: " + update);

	}
	public void updateClassification(String update){

		jTextField5.setText("Classification: " + update);

	}
	public void updateIsAboveHorizon(String update){

		jTextField6.setText("Above Horizon? " + update);

	}
	public void updateI(String update){

		jTextField7.setText("i [Degrees]: " + update);

	}
	public void updateEccentricity(String update){

		jTextField8.setText("Eccentricity: " + update);

	}
	public void updateOmega(String update){

		jTextField9.setText("omega [Degrees]: " + update);

	}
	public void updateW (String update){

		jTextField10.setText("W [Degrees]: " + update);

	}
	public void updateM(String update){

		jTextField11.setText("M  [Degrees]: " + update);

	}
	public void updateN(String update){

		jTextField12.setText("N [Revs per day]: " + update);

	}






	//***********************************************************
	private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
	}

	private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
	}

	private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
	}

	public void updateClock(String time){

		jTextClockField.setText("TIME: " + time);
	}

	public void updateChronometer(String time){
		jTextChronometerField.setText("SAT VISIBLE IN: " + time);
	}

	public void init() {
		this.setVisible(true);
	}

	public MainGUI getMainGUI(){
		return mainGUI;
	}




	/**
	 * Metodo que adiciona texto na JTextArea
	 * @param text
	 */
	public void appendText (String text){
		date = new Date();
		String dateAux = dateFormat.format(date);
		textAreaInfo.setFont(new Font("Arial",Font.BOLD,12));
		textAreaInfo.append(dateAux+": ");
		textAreaInfo.append(text+"\n");

	}

	public void clearText(){
		textAreaInfo.setText("");
	}

	public ArduinoConnector getArduinoConnector(){
		return ac;
	}

	public Orbit getOrbit(){
		return satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit();
	}

	public MovingAntenna getMovingAntenna(){
		return ma;
	}

	public boolean isSatelliteAboveHorizon(){
		return satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().isSatelliteVisible();
	}

	public double getFirstAzimuth(){
		return satelliteComboBox.getItemAt(satelliteComboBox.getSelectedIndex()).getOrbit().getFirstAzimuth();
	}

	public JButton getArduinoConnectorButton(){
		return connectArduinoButton;
	}




}
